package com.handinfo.android.game.skill;

import java.io.DataInputStream;
import java.io.IOException;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.game.IIcon;
import com.handinfo.android.game.IShortcutObject;
import com.handinfo.android.game.IconManger;
import com.handinfo.android.game.Role;

public class RoleCombatSkill extends CombatSkill implements IIcon, IShortcutObject
{
	public String m_id;
	public String m_name; // 名称
	public String m_description;
	public int m_level = 0; // 当前级别
	public int m_levelMax = 0; // 最大级别
	public int m_need_sp; // 升级所需修为

	private Bitmap m_icon;
	public int m_consumption = 0; // 技能消耗
	public int m_cd = 0; // CD
	public int m_range = 0; // 攻击范围
	
	
	public long m_lastUsedTime = 0; // CD计数器
	
	public int m_learn_lv=0;//学习等级
	
	public RoleCombatSkill m_next_lvl_skill;
	

	/**
	 * 加载
	 * 
	 * @param dis
	 * @throws IOException
	 */
	public synchronized void read(DataInputStream dis) throws IOException
	{
//		m_id = dis.readUTF();
		m_name = dis.readUTF();
		m_description = dis.readUTF();
		m_level = dis.readByte();
		m_levelMax = dis.readByte();
		m_need_sp = dis.readInt();
		m_icon = IconManger.getInstance().getIcon(dis.readShort());
		m_type = dis.readByte();
		m_targetType = dis.readByte();
		m_consumption = dis.readInt();
		m_cd = dis.readInt();
		m_castingTime = dis.readShort();
		m_range = dis.readShort();
		
		m_castSrcActionId = dis.readByte();
		m_castSrcEffectId = dis.readByte();
		m_castSrcSoundId = dis.readByte();
		
		m_singActionId = dis.readByte();
		m_singEffectId = dis.readByte();
		m_singSoundId = dis.readByte();
		
		m_castTgtEffectId = dis.readByte();
		m_castTgtSoundId = dis.readByte();
		
		m_castProcEffectId = dis.readByte();
		m_castProcSoundId = dis.readByte();
		
		m_castSpecEffectId = dis.readByte();
		m_castSpecSoundId = dis.readByte();
		
		m_learn_lv=dis.readInt();
		
		boolean hasNext = dis.readByte() != 0;
		if (!hasNext) {
			if (m_next_lvl_skill != null) {
				m_next_lvl_skill.dispose();
				m_next_lvl_skill = null;
			}
			return;
		}
		
		m_next_lvl_skill = new RoleCombatSkill();
		m_next_lvl_skill.m_id = dis.readUTF();
		m_next_lvl_skill.m_name = dis.readUTF();
		m_next_lvl_skill.m_description = dis.readUTF();
		m_next_lvl_skill.m_level = dis.readByte();
		m_next_lvl_skill.m_levelMax = dis.readByte();
		m_next_lvl_skill.m_need_sp = dis.readInt();
		int iconId = dis.readShort();
		m_next_lvl_skill.m_type = dis.readByte();
		m_next_lvl_skill.m_targetType = dis.readByte();
		m_next_lvl_skill.m_consumption = dis.readInt();
		m_next_lvl_skill.m_cd = dis.readInt();
		m_next_lvl_skill.m_castingTime = dis.readShort();
		m_next_lvl_skill.m_range = dis.readShort();
		
		m_next_lvl_skill.m_castSrcActionId = dis.readByte();
		m_next_lvl_skill.m_castSrcEffectId = dis.readByte();
		m_next_lvl_skill.m_castSrcSoundId = dis.readByte();
		
		m_next_lvl_skill.m_singActionId = dis.readByte();
		m_next_lvl_skill.m_singEffectId = dis.readByte();
		m_next_lvl_skill.m_singSoundId = dis.readByte();
		
		m_next_lvl_skill.m_castTgtEffectId = dis.readByte();
		m_next_lvl_skill.m_castTgtSoundId = dis.readByte();
		
		m_next_lvl_skill.m_castProcEffectId = dis.readByte();
		m_next_lvl_skill.m_castProcSoundId = dis.readByte();
		
		m_next_lvl_skill.m_castSpecEffectId = dis.readByte();
		m_next_lvl_skill.m_castSpecSoundId = dis.readByte();
		
		m_next_lvl_skill.m_learn_lv=dis.readInt();		
	}

	/**
	 * 释放
	 */
	public synchronized void dispose()
	{
		m_name = null;
	}

	/**
	 * 技能冷却计时
	 */
	public void cooldown()
	{
	}
	
	
	

	public void drawIcon(DWGraphics g, int x, int y, int w, int h, int anchor)
	{
		if (m_icon != null) {
			w = m_icon.getWidth();
			h = m_icon.getHeight();
			if ((anchor & DWGraphics.RIGHT) != 0)
				x -= w;
			if ((anchor & DWGraphics.HCENTER) != 0)
				x -= w / 2;
			if ((anchor & DWGraphics.BOTTOM) != 0)
				y -= h;
			if ((anchor & DWGraphics.VCENTER) != 0)
				y -= h / 2;
//			g.setClip(x, y, w, h);
			g.drawBitmap(m_icon, x, y, DWGraphics.TOP | DWGraphics.LEFT);
			//绘制CD计时蒙版
			if (m_cd > 0) {
				long time = System.currentTimeMillis() - m_lastUsedTime;
				long cd = m_cd * DWGameManager.SECOND;
				if (time < cd) {
					g.setColor(SkillConstants.COLOR_CD_COVER);
					g.fillArc(x, y, w, h, -90 + time * 360 / cd, 360 - time * 360 / cd);
				}
//				g.fillArc(x, y, w, h, 90 - (m_cd - m_cd_counter) * 360 / m_cd, -m_cd_counter * 360 / m_cd);
			}
		}
	}

	public void drawIcon(DWGraphics g, int x, int y, int w, int h, float scaleX, float scaleY, int anchor)
	{
		if (m_icon != null) {
			float rw = w * scaleX;
			float rh = h * scaleY;
			if ((anchor & DWGraphics.RIGHT) != 0)
				x -= rw;
			if ((anchor & DWGraphics.HCENTER) != 0)
				x -= rw / 2;
			if ((anchor & DWGraphics.BOTTOM) != 0)
				y -= rh;
			if ((anchor & DWGraphics.VCENTER) != 0)
				y -= rh / 2;
//			g.setClip(x, y, (int) rw, (int) rh);
			g.drawBitmap(m_icon, x, y, rw, rh, DWGraphics.TOP | DWGraphics.LEFT);
			//绘制CD计时蒙版
			if (m_cd > 0) {
				long time = System.currentTimeMillis() - m_lastUsedTime;
				long cd = m_cd * DWGameManager.SECOND;
				if (time < cd) {
					g.setColor(SkillConstants.COLOR_CD_COVER);
					g.fillArc(x, y, rw, rh, -90 + time * 360 / cd, 360 - time * 360 / cd);
				}
			}
		}
	}

	@Override
	public String toString()
	{
		return "RoleCombatSkill [m_id=" + m_id + ", m_name=" + m_name + ", m_level=" + m_level + "]";
	}

	public boolean doShortcutAction()
	{
		return true;
	}

	public boolean doShortcutAction(Role role)
	{
		if (role.canUseCombatSkill(this, role.m_targetActor)) {
			role.useCombatSkill(this);
			return true;
		}
		else{
			if (m_type == SkillConstants.吟唱 || m_type == SkillConstants.瞬发) {
				role.initAutoAttack(this);
			}
			return false;
		}
	}

}
